<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\model\store;

use app\admin\service\store\User as StoreUserService;
use app\common\library\helper;
use app\common\model\store\Setting as SettingModel;
use app\common\model\store\User as StoreUserModel;
use app\common\service\store\User as UserService;
use app\store\model\User as UserModel;
use think\facade\Db;

/**
 * 商家用户模型
 * Class StoreUser
 * @package app\store\model
 */
class User extends StoreUserModel {
	/**
	 * 隐藏的字段
	 * @var array
	 */
	protected $hidden = [
		'password',
	];

	// 用户登录token
	private $token;

	/**
	 * 商家用户登录
	 * @param array $data
	 * @return array|bool|null|\think\Model
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function login(array $data) {
		// 验证用户名密码是否正确
		if (!$userInfo = $this->getUserInfoByLogin($data)) {
			return false;
		}
		// 验证商城状态是否正常
		if (empty($userInfo['store']) || $userInfo['store']['is_delete']) {
			$this->error = '登录失败, 未找到当前商城信息';
			return false;
		}
		if ($userInfo['store']['is_recycle']) {
			$this->error = '登录失败, 当前商城已删除';
			return false;
		}
		// 记录登录状态, 并记录token
		$this->token = StoreUserService::login($userInfo->toArray());
		return $userInfo;
	}

	/**
	 * 返回生成的token
	 * @return mixed
	 */
	public function getToken() {
		return $this->token;
	}

	/**
	 * 获取登录用户信息
	 * @param array $data
	 * @return array|bool|null|\think\Model
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	private function getUserInfoByLogin(array $data) {
		// 用户信息
		$useInfo = static::withoutGlobalScope()
			->with(['store'])
			->where('user_name', '=', trim($data['username']))
			->where('is_delete', '=', 0)
			->find();

		if (empty($useInfo) || $useInfo['is_delete']) {
			$this->error = '登录失败, 该用户不存在或已删除';
			return false;
		}
		// 验证密码是否正确
		if (!password_verify($data['password'], $useInfo['password'])) {
			$this->error = '登录失败, 用户名或密码错误';
			return false;
		}

		return $useInfo;
	}

	/**
	 * 获取用户列表
	 * @param array $param
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getList($param = []) {
		// 查询模型
		$query = $this->getNewQuery();
		// 查询参数
		$params = $this->setQueryDefaultValue($param, ['search' => '']);
		// 关键词搜索
		!empty($params['search']) && $query->where('c.company_name|a.user_name|a.real_name', 'like', "%{$params['search']}%");

		$store_user = UserService::getLoginInfo();
		$store_user = $store_user['user'];

		if ($store_user['company_id'] > 0) {
			$query->where('a.company_id', $store_user['company_id']);
		}

		// 查询列表记录
		$list = $query->alias('a')->with(['role', 'company'])
			->join('shiyao_company c', 'c.company_id=a.company_id')
			->where('a.is_delete', '=', '0')
			->order(['c.company_name' => 'asc', 'a.create_time' => 'desc'])
			->paginate(15);
		// 整理所有角色id
		foreach ($list as &$item) {
			$item['roleIds'] = helper::getArrayColumn($item['role'], 'role_id');
		}
		return $list;
	}

	public function getListAll($param = []) {
		// 查询模型
		$query = $this->getNewQuery();
		// 查询参数
		$params = $this->setQueryDefaultValue($param, ['search' => '']);
		// 关键词搜索
		!empty($params['search']) && $query->where('user_name|real_name', 'like', "%{$params['search']}%");

		$store_user = UserService::getLoginInfo();
		$store_user = $store_user['user'];

		if ($store_user['company_id'] > 0) {
			$query->where('company_id', $store_user['company_id']);
		}

		// 查询列表记录
		$list = $query->with(['role', 'company'])
			->where('is_delete', '=', '0')
			->where('store_user_id', '>', 10001)
			->order(['sort' => 'asc', 'create_time' => 'desc'])
			->select()->toArray();

		// 整理所有角色id
		foreach ($list as &$item) {
			$role = array_column($item['role'], 'role_name');
			$item['role_label'] = implode(',', $role);
			$item['company_name'] = $item['company'] ? $item['company']['company_name'] : '';
		}
		return $list;
	}

	/**
	 * 新增记录
	 * @param array $data
	 * @return bool
	 */
	public function add(array $data) {
		$password = SettingModel::getItem('trade', 10001)['order']['password'];
		$data['user_name'] = strtolower($data['user_name']);
		if (self::checkExist($data['user_name'])) {
			$this->error = '账号已存在';
			return false;
		}
		if (empty($data['password'])) {
			$data['password'] = $password;
		}

		if (empty($data['roles'])) {
			$this->error = '请选择所属角色';
			return false;
		}
		// 整理数据
		$data['pwd'] = $data['password'];
		$data['password'] = encryption_hash($data['password']);

		$data['store_id'] = self::$storeId;
		$data['is_super'] = 0;
		$data['grade_id'] = 3; //pm

		// 事务处理
		$this->transaction(function () use ($data) {
			// 新增管理员记录
			$this->save($data);
			// 新增角色关系记录
			UserRole::increased((int) $this['store_user_id'], $data['roles']);
			// 新增前台账号
			UserModel::increased($data);
		});
		return true;
	}

	/**
	 * 更新记录
	 * @param array $data
	 * @return bool
	 */
	public function edit(array $data) {

		$data['grade_id'] = 3; //pm
		$data['user_name'] = strtolower($data['user_name']);
		if ($this['user_name'] !== $data['user_name']
			&& self::checkExist($data['user_name'])) {
			$this->error = '账号已存在';
			return false;
		}

		if (empty($data['roles']) && !$this['is_super']) {
			$this->error = '请选择所属角色';
			return false;
		}
		
		// if (!empty($data['password'])) {
		// 	$data['pwd'] = $data['password'];
		// 	$data['password'] = encryption_hash($data['password']);

		// } else {
		// 	unset($data['password']);
		// }
		$this->transaction(function () use ($data) {
			// 更新管理员记录
			$this->save($data);
			// 更新角色关系记录
			!$this['is_super'] && UserRole::updates((int) $this['store_user_id'], $data['roles']);

			// 新增前台账号
			UserModel::updates($data);
		});
		return true;
	}

	/**
	 * 软删除
	 * @return false|int
	 */
	public function setDelete() {
		if ($this['is_super']) {
			$this->error = '超级管理员不允许删除';
			return false;
		}
		return $this->transaction(function () {
			// 删除对应的角色关系
			UserRole::deleteAll([['store_user_id', '=', (int) $this['store_user_id']]]);
			Db::name('user')->where('mobile', $this['user_name'])->update(['is_delete' => 1]);
			return $this->save(['is_delete' => 1]);
		});
	}

	/**
	 * 更新当前管理员信息
	 * @param array $data
	 * @return bool
	 */
	public function renew(array $data) {
		if (isset($data['bank_num'])) {
			Db::name('user')->where('mobile', $data['user_name'])
				->update(['bank_num' => $data['bank_num'], 'bank_name' => $data['bank_name'], 'alipay' => $data['alipay']]);
		}

		if (!empty($data['password']) && ($data['password'] !== $data['password_confirm'])) {
			$this->error = '确认密码不正确';
			return false;
		}
		if ($this['user_name'] !== $data['user_name']
			&& self::checkExist($data['user_name'])) {
			$this->error = '用户名已存在';
			return false;
		}
		if (!empty($data['password'])) {
			Db::name('user')->where('mobile', $this['user_name'])->update(['password' => $data['password']]);
			$data['pwd'] = $data['password'];
			$data['password'] = encryption_hash($data['password']);
		}

		// 更新管理员信息
		if ($this->save($data) === false) {
			return false;
		}
		// 更新登录状态
		StoreUserService::update($this->toArray());
		return true;
	}

}
